package oa.hleast.xswl.common.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import oa.hleast.xswl.pojo.vo.IdCardVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class BaiduUtils {
    //设置APPID/AK/SK
    @Value("${baidu_ai.app_id}")
    public static String APP_ID = "23317835";

    @Value("${baidu_ai.api_key}")
    public static String API_KEY = "P8q2eYOhvV0uhk4LBLgrfgpl";

    @Value("${baidu_ai.secret_key}")
    public static String SECRET_KEY = "BA8ZMNOgSEIGodhIyG7Elrqx76kUOvtN";

    @Value("${baidu_ai.api_url}")
//    public String API_URL = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant";
    public static String API_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
//    public static String API_URL = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";


    public static String API_AUTH = "https://aip.baidubce.com/oauth/2.0/token";

    public static final Boolean FRONT = true;
    public static final Boolean BACK = false;

    public static void main(String[] args) {
        File file = new File("C:\\Users\\roy\\Pictures\\sfzfm.jpg");
        JSONObject result = recognizePath(file.getAbsolutePath(), BACK);
        System.out.print(result);
//        if (IDUtils.isIDNumber(result)) {
//            System.out.println("-------------正确");
//        } else {
//            System.out.println("-------------错误");
//        }
    }

    public static Map<String,String> checkIdCard(IdCardVO idCardVO) {
        String idCardNum = idCardVO.getIdCardNum();
        String fullname = idCardVO.getFullname();
        String imagerFrontCard = idCardVO.getImagerFrontCard();
        String imagerBackCard = idCardVO.getImagerBackCard();
        Assert.isTrue(!StrUtil.isEmpty(idCardNum), "身份证号不能为空");
        Assert.isTrue(!StrUtil.isEmpty(fullname), "姓名不能为空");
        Assert.isTrue(!StrUtil.isEmpty(imagerFrontCard), "身份证正面图片地址不能为空");
        Assert.isTrue(!StrUtil.isEmpty(imagerBackCard), "身份证反面图片地址不能为空");
        Assert.isTrue(IDUtils.isIDNumber(idCardNum), "传入身份证号码不合法");
        JSONObject frontInfo = recognizeUrl(imagerFrontCard, FRONT);
        JSONObject backInfo = recognizeUrl(imagerBackCard, BACK);
        String brithday = frontInfo.getJSONObject("出生").getStr("words");
        String gender = frontInfo.getJSONObject("性别").getStr("words");
        String name = frontInfo.getJSONObject("姓名").getStr("words");
        String race = frontInfo.getJSONObject("民族").getStr("words");
        String address = frontInfo.getJSONObject("住址").getStr("words");
        String idNum = frontInfo.getJSONObject("公民身份号码").getStr("words");
        String issue = backInfo.getJSONObject("签发日期").getStr("words");
        String expire = backInfo.getJSONObject("失效日期").getStr("words");
        String office = backInfo.getJSONObject("签发机关").getStr("words");
        Assert.isTrue(fullname.equals(name),"传入名称与图中身份证名称不一致");
        Assert.isTrue(idCardNum.equals(idNum),"传入身份证号与图中身份证号不一致");
        Map<String,String> result = new HashMap<>();
        result.put("brithday",brithday);
        result.put("gender",gender);
        result.put("name",name);
        result.put("race",race);
        result.put("address",address);
        result.put("idNum",idNum);
        result.put("issue",issue);
        result.put("expire",expire);
        result.put("office",office);
        return result;
    }

    public static JSONObject recognizeUrl(String path, boolean side) {
        return recognize(path, false, side);
    }

    public static JSONObject recognizePath(String path, boolean side) {
        return recognize(Base64.encode(new File(path)), true, side);
    }

    public static JSONObject recognize(String imgInfo, boolean base64Flag, boolean side) {
        String body = HttpRequest.post(API_AUTH)
                .form("grant_type", "client_credentials")
                .form("client_id", API_KEY)
                .form("client_secret", SECRET_KEY)
                .execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        String accessToken = jsonObject.getStr("access_token");
        Map<String, Object> paramMap = new HashMap<>();
        if (base64Flag) {
            paramMap.put("image", imgInfo);
        } else {
            paramMap.put("url", imgInfo);
        }
        paramMap.put("id_card_side", side ? "front" : "back");
//        paramMap.put("baike_num", 5);
        String result2 = HttpRequest.post(API_URL + "?access_token=" + accessToken)
                .header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded")
                .form(paramMap)
                .execute().body();
        JSONObject jsonObject1 = JSONUtil.parseObj(result2);
        int num = jsonObject1.getInt("words_result_num");
        JSONObject object = jsonObject1.getJSONObject("words_result");
//        String result = "";
//        for (int i = 0; i < num; i++) {
//            JSONObject result3 = (JSONObject) jsonArray.get(i);
//            result = result3.getStr("words");
//        }

        return object;
    }
}